﻿using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Management.Domain.IdentityModel;
using Microsoft.IdentityModel.Tokens;

namespace Management.HttpApi.Host.Helpers
{
    public static class TokenHelper
    {
        /// <summary>
        /// 基于JWT Bearer
        /// </summary>
        /// <returns>Token</returns>
        // public static string GenerateToken(UserProfile userProfile)
        // {
        //     try
        //     {
        //         string iss = "TangXiaWen";
        //         string aud = "myApi";
        //
        //         IEnumerable<Claim> claims = new Claim[]
        //         {
        //             new Claim(JwtModel.Id,userProfile.Id.ToString()),
        //             new Claim(JwtModel.Name,userProfile.Name),
        //             new Claim(JwtModel.Gender,userProfile.Gender.ToString()),
        //             new Claim(JwtModel.Mobilephone,userProfile.Mobilephone),
        //             new Claim(JwtModel.Email,userProfile.Email??JwtModel.Email),
        //             new Claim(JwtModel.Address,userProfile.Address??JwtModel.Address),
        //             new Claim(JwtModel.HeadImgUrl,userProfile.HeadImgUrl??JwtModel.HeadImgUrl)
        //         };
        //
        //         var nbf = DateTime.UtcNow;
        //         var Exp = DateTime.UtcNow.AddSeconds(1000);
        //         string sign = "q2xiARx$4x3TKqBJ";
        //         var secret = Encoding.UTF8.GetBytes(sign);
        //         var key = new SymmetricSecurityKey(secret);
        //         var signcreds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
        //         var jwt = new JwtSecurityToken(issuer: iss, audience: aud, claims: claims, notBefore: nbf, expires: Exp, signingCredentials: signcreds);
        //         var JwtHander = new JwtSecurityTokenHandler();
        //         var token = JwtHander.WriteToken(jwt);
        //         return ("Bearer " + token);
        //     }
        //     catch (Exception ex)
        //     {
        //         throw new Exception("生成Token错误" + ex.Message);
        //     }
        // }
        public static string GenerateToken()
        {
            try
            {
                string iss = "TangXiaWen";
                string aud = "myApi";

                IEnumerable<Claim> claims = new Claim[]
                {
                    new Claim(JwtModel.Id,"admin"),
                    new Claim(JwtModel.Name,"admin"),
                    new Claim(JwtModel.Gender,"男"),
                    new Claim(JwtModel.Mobilephone,"12"),
                    new Claim(JwtModel.Email,"12"),
                    new Claim(JwtModel.Address,"Address"),
                    new Claim(JwtModel.HeadImgUrl,"url")
                };

                var nbf = DateTime.UtcNow;
                var Exp = DateTime.UtcNow.AddSeconds(1000);
                string sign = "q2xiARx$4x3TKqBJ";
                var secret = Encoding.UTF8.GetBytes(sign);
                var key = new SymmetricSecurityKey(secret);
                var signcreds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                var jwt = new JwtSecurityToken(issuer: iss, audience: aud, claims: claims, notBefore: nbf, expires: Exp, signingCredentials: signcreds);
                var JwtHander = new JwtSecurityTokenHandler();
                var token = JwtHander.WriteToken(jwt);
                return ("Bearer " + token);
            }
            catch (Exception ex)
            {
                throw new Exception("生成Token错误" + ex.Message);
            }
        }
    }
}
